package scau.zzf.base.util;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;


/**
 * Created by Administrator on 2016/9/7.
 */
public class PasswordHelper {
    private static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    private static String algorithmName = "md5";
    private static int hashIterations = 2;

    public void setRandomNumberGenerator(RandomNumberGenerator randomNumberGenerator) {
        this.randomNumberGenerator = randomNumberGenerator;
    }

    public void setAlgorithmName(String algorithmName) {
        this.algorithmName = algorithmName;
    }

    public void setHashIterations(int hashIterations) {
        this.hashIterations = hashIterations;
    }

    public static String getSalt() {
        return randomNumberGenerator.nextBytes().toHex();
    }

    public static String encryptPassword(String password, String credentialsSalt) {


        String newPassword = new SimpleHash(algorithmName, password, ByteSource.Util.bytes(credentialsSalt), hashIterations).toHex();

       return newPassword;
    }
//
//
//    public static boolean comparePassword(Users user, String inputPassword){
//
//        String newPassword = new SimpleHash(
//                algorithmName,
//                inputPassword,
//                ByteSource.Util.bytes(user.getCredentialsSalt()),
//                hashIterations
//        ).toHex();
//        if(user.getPassword().equals(newPassword)){
//            return true;
//        }else {
//            return false;
//        }
//    }


}
